﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ATEGo
{
    /// <summary>
    /// 存放扩展方法的类
    /// </summary>
    public static class Extend
    {
        /// <summary>
        /// 复制一个新的字典
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> Copy<TKey, TValue>(this Dictionary<TKey, TValue> dic)
        {
            Dictionary<TKey, TValue> dic2 = new Dictionary<TKey, TValue>();
            foreach (var key in dic.Keys)
            {
                dic2[key] = dic[key];
            }
            return dic2;
        }
        /// <summary>
        /// 处理时间成毫秒数，支持10\1,000\10ms\10s\10min\0.5h的写法
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static int TimeToMS(this string waitTime)
        {
            double time = 0;
            if (waitTime.EndsWith("ms"))
            {
                time = waitTime.Replace("ms", "").JsonToDouble();
            }
            else if (waitTime.EndsWith("s"))
            {
                time = waitTime.Replace("s", "").JsonToDouble() * 1000;
            }
            else if (waitTime.EndsWith("min") || waitTime.EndsWith("m"))
            {
                time = waitTime.Replace("min", "").Replace("m", "").JsonToDouble() * 60000;
            }
            else if (waitTime.EndsWith("h"))
            {
                time = waitTime.Replace("h", "").JsonToDouble() * 3600000;
            }
            else
            {
                time = waitTime.JsonToDouble();
            }
            return Convert.ToInt32(time);
        }
        /// <summary>
        /// Json文件中的str转成int，并去除其中的,分隔符
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static int JsonToInt32(this string para)
        {
            try
            {
                return Convert.ToInt32(para.Trim().Replace(",", ""));
            }
            catch
            {
                throw new Exception($"[{para}]转换int类型失败！");
            }
        }

        public static double JsonToDouble(this string para)
        {
            try
            {
                return Convert.ToDouble(para.Trim().Replace(",", ""));
            }
            catch
            {
                throw new Exception($"[{para}]转换double类型失败！");
            }
        }

        public static bool JsonToBoolean(this string para)
        {
            try
            {
                return Convert.ToBoolean(para.Trim());
            }
            catch
            {
                throw new Exception($"[{para}]转换bool类型失败！");
            }
        }

        public static bool JsonFailedRetryGoon(this string para)
        {
            para = para.Trim().ToLower();
            if (para == "retry")
            {
                return true;
            }
            else if(para =="goon")
            {
                return false;
            }
            else
            {
                return JsonToBoolean(para);
            }
        }

        public static bool JsonIfLast(this string para)
        {
            para = para.Trim().ToLower();
            if (para == "success")
            {
                return true;
            }
            else if (para == "fail")
            {
                return false;
            }
            else
            {
                return JsonToBoolean(para);
            }
        }

        /// <summary>
        /// 替换命令里面的变量
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string ReplaceParas(this string cmd, Executor e,bool isReg=false)
        {
            return e.ReplaceCmdParas(cmd,isReg);
        }
        /// <summary>
        /// 把字符串改成正则表达式，需要转义，不完全
        /// </summary>
        /// <param name="ori"></param>
        /// <returns></returns>
        public static string ToReg(this string ori)
        {
            return ori.Replace("\\","\\\\").Replace(".", "\\.").Replace("*", "\\*").Replace("[", "\\[").Replace("]", "\\]");
        }
    }
}
